package com.zy.cat.controller.game.v1;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.common.utils.DateAPIUtils;
import com.zy.cat.dao.Dao;
import com.zy.cat.entity.game.TrunDrawCountHistory;
import com.zy.cat.entity.game.TrunDrawExtraRewardInfo;
import com.zy.cat.entity.game.TrunDrawHistory;
import com.zy.cat.entity.game.repository.TrunDrawCountHistoryRepository;
import com.zy.cat.entity.game.repository.TrunDrawHistoryRepository;
import com.zy.cat.entity.game.resp.TrunDrawExtraRewardResp;
import com.zy.cat.entity.game.resp.TrunDrawHistoryResp;
import com.zy.cat.entity.game.resp.TrunDrawResp;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatGameService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "9catGameTurn", tags = { "小游戏-大转盘-开放API" }, description = "小游戏-大转盘-开放接口")
@RestController
@RequestMapping("/v1/game/turn")
public class CatGameTurnController {

	@Autowired
	CacheService cacheService;

	@Autowired
	CatGameService catGameService;
	@Autowired
	TrunDrawHistoryRepository trunDrawHistoryRepository;
	
	@Autowired
	Dao<TrunDrawHistory> trunDrawHistoryDao;

	@PostMapping("/info")
	@ApiOperation(value = "大转盘初始化信息", notes = "每次进入请求，返回今日游戏进度等信息", consumes = "application/x-www-form-urlencoded", position = 1, response = TrunDrawResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_pid_utoken_uuid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "uuid_pid_utoken_uuid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getTurnInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<TrunDrawResp>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<TrunDrawResp>> result = new Callable<RSPE<TrunDrawResp>>() {
			@Override
			public RSPE<TrunDrawResp> call() throws Exception {
				return new RSPE<TrunDrawResp>(catGameService.getTurnInfo(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/draw")
	@ApiOperation(value = "大转盘进行抽奖", notes = "每次进入请求，返回今日礼包、游戏进度等信息", consumes = "application/x-www-form-urlencoded", position = 2, response = TrunDrawResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_utoken_pid_rtype_pid_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_utoken_pid_rtype_pid_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getTurnDraw(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if ((Long)catGameService.getTurnInfo(rtype, pid).getDcount()<1) {
			return new RSPE<Object>(-3, "当前抽奖次数已不足！");
		}
		Callable<RSPE<TrunDrawResp>> result = new Callable<RSPE<TrunDrawResp>>() {
			@Override
			public RSPE<TrunDrawResp> call() throws Exception {
				return new RSPE<TrunDrawResp>(catGameService.getTurnDraw(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/draw/double")
	@ApiOperation(value = "大转盘进行抽奖翻倍", notes = "观看广告，进行奖励翻倍,仅支持抽奖奖励的现金豆，其他不支持翻倍！", consumes = "application/x-www-form-urlencoded", position = 3, response = TrunDrawHistoryResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "fid", value = "中奖翻倍ID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_fid_utoken_pid_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_fid_utoken_pid_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getTurnDrawDouble(Integer rtype, String pid, String fid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		TrunDrawHistory draw = trunDrawHistoryRepository.findByPidAndFid(pid, fid);
		if (draw == null || draw.getDstatus() != 1) {
			return new RSPE<Object>(-3, "抽奖信息不存在！");
		}
		if (draw.getZtype() != 0) {
			return new RSPE<Object>(-4, "此奖励不支持翻倍！");
		}
		if (draw.getFtype() == 1) {
			return new RSPE<Object>(-5, "已经翻倍，请不要重复请求！");
		}
		Callable<RSPE<TrunDrawHistoryResp>> result = new Callable<RSPE<TrunDrawHistoryResp>>() {
			@Override
			public RSPE<TrunDrawHistoryResp> call() throws Exception {
				return new RSPE<TrunDrawHistoryResp>(catGameService.getTurnDrawDouble(rtype,draw));
			}
		};
		return result;
	}
	
	@PostMapping("/draw/reward")
	@ApiOperation(value = "大转盘领取额外奖励", notes = "点击宝箱领取额外奖励！", consumes = "application/x-www-form-urlencoded", position = 3, response = TrunDrawExtraRewardResp.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户PID", example = "5371329791", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "code", value = "领取宝箱code(5/30/60/100)", example = "5", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(uuid_code_utoken_pid_rtype_uuid_time)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "uuid_code_utoken_pid_rtype_uuid", MD5 = "token")
	@CheckDevPermission
	public Object getTurnDrawExtraReward(Integer rtype, String pid, String code, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (StringUtils.isEmpty(code)) {
			return new RSPE<Object>(-3, "领取宝箱code参数为空！");
		}
		int[] extraBeanNumArray= {80,300,500,800};
		int[] extraCountArray= {5,30,60,100};
		int index = Arrays.binarySearch(extraCountArray,Integer.parseInt(code));
		if (index<0) {
			return new RSPE<Object>(-6, "code参数值不存在！");
		}
		//获取已抽奖次数,判断是否可领取此code对应的额外奖励
		TrunDrawHistory history = trunDrawHistoryDao.selectOne(new TrunDrawHistory(pid,extraBeanNumArray[index]), "getTodayExtraRewardHistory");
		if (history==null) {
			return new RSPE<Object>(-4, "您还未达到"+code+"次抽奖次数！");
		}
		if (history.getDstatus()==1) {
			return new RSPE<Object>(-5, "已领取此奖励！");
		}
		Callable<RSPE<TrunDrawExtraRewardResp>> result = new Callable<RSPE<TrunDrawExtraRewardResp>>() {
			@Override
			public RSPE<TrunDrawExtraRewardResp> call() throws Exception {
				TrunDrawExtraRewardResp extraReward = catGameService.getTurnDrawExtraReward(rtype,history,code);
				if (extraReward==null) {
					return new RSPE<TrunDrawExtraRewardResp>(0, "领取失败");
				}
				return new RSPE<TrunDrawExtraRewardResp>(extraReward);
			}
		};
		return result;
	}
}
